<?php

namespace app\shop\controller;

use think\Controller;

class Coupon extends Controller
{

    public function _initialize()
    {
        parent::_initialize();

        $this->BusinessModel = model('Business.Business');
        $this->CouponModel = model('Coupon.Coupon');
        $this->CouponReceiveModel = model('Coupon.Receive');
    }

    // 列表页
    public function index()
    {
        if ($this->request->isPost())
        {

            $data = $this->CouponModel->where(['status' => '1'])->order('createtime desc')->select();

            $this->success('', '', $data);
        }
    }

    // 详情页
    public function info()
    {
        if ($this->request->isPost())
        {
            $cid = $this->request->param('cid', 0, 'trim');
            $busid = $this->request->param('busid', 0, 'trim');

            $info = $this->CouponModel->where(['status' => '1'])->find($cid);

            if (!$info)
            {
                $this->error('优惠券活动已结束');
                exit;
            }

            $info['receive_text'] = false;

            $business = $this->BusinessModel->find($busid);

            if ($business)
            {
                $receive = $this->CouponReceiveModel->where(['busid' => $busid, 'status' => 1, 'cid' => $cid])->find();

                $info['receive_text'] = $receive ? true : false;
            }

            $this->success('', '', $info);
        }
    }

    //优惠券领取
    public function receive()
    {
        if ($this->request->isPost())
        {
            $cid = $this->request->param('cid', 0, 'trim');
            $busid = $this->request->param('busid', 0, 'trim');

            $info = $this->CouponModel->where(['status' => '1'])->find($cid);

            if (!$info)
            {
                $this->error('优惠券活动已结束');
                exit;
            }

            $business = $this->BusinessModel->find($busid);
            if (!$business)
            {
                $this->error('用户不存在');
                exit;
            }


            $receiveinfo = $this->CouponReceiveModel->where(['cid' => $cid, 'busid' => $busid, 'status' => '1'])->find();

            if ($receiveinfo)
            {
                $this->error('优惠券已领取，无需重复领取');
                exit;
            }

            $this->CouponReceiveModel->startTrans();
            $this->CouponModel->startTrans();

            $CouponReceiveData = [
                'cid' => $cid,
                'busid' => $busid,
                'status' => '1'
            ];

            // 加校验器
            $CouponReceiveStatus = $this->CouponReceiveModel->save($CouponReceiveData);

            if ($CouponReceiveStatus === false)
            {
                $this->error($this->CouponReceiveModel->getError());
                exit;
            }

            $total = bcsub($info['total'], 1);

            $CouponData = [
                'id' => $cid,
                'total' => $total
            ];

            $CouponStatus = $this->CouponModel->isUpdate(true)->save($CouponData);

            if ($CouponStatus === false)
            {
                $this->CouponReceiveModel->rollback();
                $this->error($this->CouponModel->getError());
                exit;
            }

            if ($CouponReceiveStatus === false || $CouponStatus === false)
            {
                $this->CouponReceiveModel->rollback();
                $this->CouponModel->rollback();
                $this->error('领取失败');
                exit;
            }
            else
            {
                $this->CouponReceiveModel->commit();
                $this->CouponModel->commit();
                $this->success('领取成功');
                exit;
            }
        }
    }

    // 订单优惠券
    public function order()
    {
        if ($this->request->isPost())
        {
            $busid = $this->request->param('busid', 0, 'trim');

            $business = $this->BusinessModel->find($busid);

            if (!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            $data = $this->CouponReceiveModel->with('coupon')->where(['busid' => $busid, 'receive.status' => '1'])->select();

            $this->success('', '', $data);

        }
    }
}
